package com.junzhiit.nengji.api.web.controller;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.bainaeco.framework.util.HttpClientUtils;
import com.junzhiit.nengji.api.service.OrdersService;
import com.junzhiit.nengji.api.web.vo.ResultVO;

import io.swagger.annotations.ApiOperation;

@RestController
@RequestMapping("/test")
public class TestController {
	
	@Autowired
	private OrdersService ordersService;

	@ApiOperation(value = "重置交易历史表")
	@RequestMapping(value = "reset/payHistory", method = RequestMethod.POST)
	public ResultVO add() {
		return ordersService.resetPayHistory();
	}
	
	/* public static void main(String[] args)
	    {
	        try
	        {
	            BufferedImage appletImg = ImageIO.read(new FileInputStream("D:\\BB.png"));
	            Graphics2D g2d = appletImg.createGraphics();

	            // 设置抗锯齿的属性  
	            g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
	            g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);

	            BufferedImage centerImg = ImageIO.read(new File("D:\\CC.jpg"));
	            g2d.drawImage(centerImg.getScaledInstance(centerImg.getWidth(), centerImg.getHeight(), Image.SCALE_SMOOTH), (appletImg.getWidth() - centerImg.getWidth()) / 2, (appletImg.getHeight() - centerImg.getHeight()) / 2, null);

	            // 关闭资源
	            g2d.dispose();
	            ImageIO.write(appletImg, "png", new File("D:\\DD.png"));
	        }
	        catch (Exception e)
	        {
	            e.printStackTrace();
	        }
	    }*/
	 
	 /*public static void main(String[] args) {
		//生成文件名称，这个不是必须的，可以根据自己的业务逻辑来写  
		 Random random = new Random();    
		 int rannum = (int) (random.nextDouble() * (99999 - 10000 + 1)) + 10000;  
		 SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");                 
		 String newFileName = df.format(new Date())+rannum+".png";  
		                       
		 //截取为正方形图片,长宽自定义，我自己估摸着是195，这个可以根据自己情况自己设置大小  
		 try {  
		     //此处是要替代logo的自定义图片路径，自定义图片不要用透明的或者半透明的  
		     BufferedImage image = ImageIO.read(new File("D:\\CC.jpg"));  
		     //int w = image.getWidth();  
		     //int h = image.getHeight();  
		     BufferedImage output = new BufferedImage(195, 195, BufferedImage.TYPE_INT_ARGB);  
		     Graphics2D g2 = output.createGraphics();  
		     output = g2.getDeviceConfiguration().createCompatibleImage(195, 195, Transparency.TRANSLUCENT);  
		     g2.dispose();  
		     g2 = output.createGraphics();  
		     g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);  
		     g2.fillRoundRect(0, 0,195, 195, 720, 720);  
		     g2.setComposite(AlphaComposite.SrcIn);  
		     g2.drawImage(image, 0, 0, 195, 195, null);  
		     g2.dispose();  
		     //这是生成的临时替换logo图片的保存路径                 
		     ImageIO.write(output, "png", new File("D:\\"+newFileName));  
		  } catch (IOException e) {  
		         e.printStackTrace();  
		  }  
		  //生产新的二维码图片  
		  try{  
		     //此处是小程序码的路径  
		     BufferedImage appletImg = ImageIO.read(new FileInputStream("D:\\BB.png"));  
		     Graphics2D g2d = appletImg.createGraphics();  
		   
		     // 设置抗锯齿的属性    
		     g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);  
		     g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);  
		       
		     //此处是替换logo的临时图片路径  
		     BufferedImage centerImg = ImageIO.read(new File("D:\\"+newFileName));  
		     g2d.drawImage(centerImg.getScaledInstance(centerImg.getWidth(), centerImg.getHeight(), Image.SCALE_SMOOTH), (appletImg.getWidth() - centerImg.getWidth()) / 2, (appletImg.getHeight() - centerImg.getHeight()) / 2, null);  
		   
		     // 关闭资源  
		     g2d.dispose();  
		     //生成新的二维码，覆盖原来的，此处为原小程序码路径，如需另为保存，请自定义路径  
		     ImageIO.write(appletImg, "png", new File("D:\\DD.png"));  
		  }catch (Exception e){  
		     e.printStackTrace();  
		  }  
	}*/
	 
	 
	 /** 
	     *  
	     * 根据文件id下载文件 
	     *  
	     *  
	     *  
	     * @param mediaId 
	     *  
	     *            媒体id 
	     *  
	     * @throws Exception 
	     */  
	  
	    public static InputStream getInputStream(String accessToken, String mediaId) {  
	        InputStream is = null;  
	        String url = "http://file.api.weixin.qq.com/cgi-bin/media/get?access_token="  
	                + accessToken + "&media_id=" + mediaId;  
	        try {  
	            URL urlGet = new URL(url);  
	            HttpURLConnection http = (HttpURLConnection) urlGet.openConnection();  
	            http.setRequestMethod("GET"); // 必须是get方式请求  
	            http.setRequestProperty("Content-Type",  
	                    "application/x-www-form-urlencoded");  
	            http.setDoOutput(true);  
	            http.setDoInput(true);  
	            System.setProperty("sun.net.client.defaultConnectTimeout", "30000");// 连接超时30秒  
	            System.setProperty("sun.net.client.defaultReadTimeout", "30000"); // 读取超时30秒  
	            http.connect();  
	            // 获取文件转化为byte流  
	            is = http.getInputStream();  
	  
	        } catch (Exception e) {  
	            e.printStackTrace();  
	        }  
	        return is;  
	  
	    }  
	  
	    /** 
	     *  
	     * 获取下载图片信息（jpg） 
	     *  
	     *  
	     *  
	     * @param mediaId 
	     *  
	     *            文件的id 
	     *  
	     * @throws Exception 
	     */  
	  
	    public static void saveImageToDisk(String accessToken, String mediaId, String picName, String picPath)  
	            throws Exception {  
	        InputStream inputStream = getInputStream(accessToken, mediaId);  
	        byte[] data = new byte[10240];  
	        int len = 0;  
	        FileOutputStream fileOutputStream = null;  
	        try {  
	            fileOutputStream = new FileOutputStream(picPath+picName+".jpg");  
	            while ((len = inputStream.read(data)) != -1) {  
	                fileOutputStream.write(data, 0, len);  
	            }  
	        } catch (IOException e) {  
	            e.printStackTrace();  
	        } finally {  
	            if (inputStream != null) {  
	                try {  
	                    inputStream.close();  
	                } catch (IOException e) {  
	                    e.printStackTrace();  
	                }  
	            }  
	            if (fileOutputStream != null) {  
	                try {  
	                    fileOutputStream.close();  
	                } catch (IOException e) {  
	                    e.printStackTrace();  
	                }  
	            }  
	        }  
	    }  
	  
	    /** 
	     * 图片下载 
	     *  
	     * @param accessToken 
	     * @param mediaId 
	     */  
	  /*  public static void getPic(String accessToken, String mediaId) {  
	        String requestUrl = "http://file.api.weixin.qq.com/cgi-bin/media/get?access_token=ACCESS_TOKEN&media_id=MEDIA_ID";  
	        requestUrl = requestUrl.replace("ACCESS_TOKEN", accessToken).replace(  
	                "MEDIA_ID", mediaId);  
	        JSONObject jsonObject = WeixinUtil.httpRequest(requestUrl, "GET", null);  
	        System.out.println(jsonObject);  
	    }  */
	  
	    public static void main(String[] args) throws Exception {  
	    /*	AccessToken accessToken = WeChatUtils.getAccessToken();
	        String mediaId = "ostSJ5d9mKrQXnnjtHqFHQSxmEZ0";  
	        String picName = "aaaa";  
	        saveImageToDisk(accessToken.getAccessToken(), mediaId, picName,"f:/");*/  
	        
	        HttpClientUtils.imageDownloader("https://wx.qlogo.cn/mmopen/vi_32/8GFEP3picCwvuOKz5q0vOCqJKe8voPBXmNtNsftHoLibg4Lkul9gHUOowTAPqzM567Oe6VWW8X5VVvwKibBDdwibcA/132","f:/aa.png");
	    }  
}
